#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<assert.h>
#include<list>
#include<stdbool.h>

using namespace std;

namespace Solution
{

	template<class T>
	class vector<T>
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin() { return _start; }
		iterator end() { return _finish; }
		const_iterator const_begin()const { return begin; }
		const_iterator const_end()const { return  end; }

		template<class T>
		vector(size_t n, const T& val = T())
		{
			while (n--)
				push_back(val);
		}
		vector(int n, int val)

		{
			while (n--)
				push_back(val);
		}

		vector() = default;


		vector(vector<T>& v)
		{
			reserve(v.capacity());
			for (auto val : v)
				push_back(val);
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			reserve(last - first);
			while (first != last)
				push_back(*first++);
		}

		~vector()
		{
			delete[]_start;
			_start = _finish = _end_of_storage;
		}

		size_t capacity() { return _end_of_storage - _start; }

		size_t size() { return _finish - _start; }

		void swap(vector<T>& v)
		{
			std::swap(v._start, _start);
			std::swap(v._finish, _finish);
			std::swap(v._end_of_storage, _end_of_storage);
		}

		vector<T>& operator=(vector<T>v) { swap(v, *this); }

		T& operator[](size_t n)
		{
			assert(n < size());
			return *(begin() + n);
		}

		const T& operator[](size_t n)const
		{
			assert(n < size());
			return *(const_begin() + n);
		}

		bool empty() { return size() == 0; }

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				int old_size = size();
				iterator new_start = new T[n];
				for (size_t i = 0; i < old_size; i++)
				{
					new_start[i] = _start[i];
				}
				delete[] _start;
				_start = new_start;
				_finish = _start + old_size;
				_end_of_storage = _start + n;

			}
		}

		void resize(size_t n)
		{
			if (n > capacity())
				reserve(n);
			else
				_finish = _start + n;
		}

		void resize(size_t n, const T& val = T())
		{
			if (n > capacity())
				reserve(n);
			else
				_finish = _start + n;
			for (auto v : *this)
				v = val;
		}

		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			*_finish++ = val;
		}
		void insert(iterator pos, const T& val)
		{
			if (_finish == _end_of_storage)
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			iterator end = _finish++;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*end = val;
		}

		iterator erase(iterator pos)
		{
			assert(!empty());
			assert(_start <= pos && pos < _finish);
			iterator begin = pos;
			while (begin < _finish)
			{
				*begin = *(begin + 1);
				begin++;
			}
			_finish--;
			return pos;
		}


		void pop_back(){_finish--;}

	private:
		iterator _start = nullptr;
		iterator _finish = __nullptr;
		iterator _end_of_storage = nullptr;
	};

	template<class Container>
	void print_container(Container con)
	{
		for (auto a : con)
			cout << a << ' ';
	}

	void test_vector01()
	{
		vector<int>v(1000,1);
		vector<int>v1(v.begin(),v.end());
		v1[0] = 12;
		list<int>l1{12, 12, 1, 212};
		print_container(l1);
	
	}
	void test_vector02()
	{
		vector<int>v(1000, 1);
		vector<int>v1(v.begin(), v.end());
		v1.resize(12, 0);
		print_container(v1);

	}
	void test_vector03()
	{
		vector<int>v(10, 1);
		vector<int>v1(v.begin(), v.end());
		v1.resize(12, 0);
		v.insert(v.begin(), 12);
		v.insert(v.end(), 12);
		v.erase(v.begin());
		v.pop_back();
		print_container(v);

	}

}
